#!/bin/bash

function prepare() {
  apt update
  #download openEuler image
  if [ -e  openEuler-1.0-aarch64-dvd.iso ]
  then  
    echo "openEuler-1.0-aarch64-dvd.iso" 
  else
    wget https://openeuler.obs.cn-south-1.myhuaweicloud.com/release/openEuler-1.0-aarch64-dvd.iso 
  fi 


  #check whether there exists the tar packet,if not then install and decompress the linux kernel 注意：如果GCC 下载过慢请访问https://pan.baidu.com/s/1vqVBVhwPL9it3gMDPjRBbA手动下载～
  if [ -e gcc-linaro-7.3.1-2018.05-x86_64_aarch64-linux-gnu.tar.xz ]
  then  
    echo "gcc-linaro-7.3.1-2018.05-x86_64_aarch64-linux-gnu.tar.xz exits, do not compile linux kernel" 
    mkdir -p /usr/ARM/toolchain/ 
    tar -xvf gcc-linaro-7.3.1-2018.05-x86_64_aarch64-linux-gnu.tar.xz -C /usr/ARM/toolchain/
    echo "export PATH=\"/usr/ARM/toolchain/gcc-linaro-7.3.1-2018.05-x86_64_aarch64-linux-gnu/bin:\$PATH\"" >> ~/.bashrc
    source ~/.bashrc
  else
    wget http://releases.linaro.org/components/toolchain/binaries/7.3-2018.05/aarch64-linux-gnu/gcc-linaro-7.3.1-2018.05-x86_64_aarch64-linux-gnu.tar.xz 
    mkdir -p /usr/ARM/toolchain/ 
    tar -xvf gcc-linaro-7.3.1-2018.05-x86_64_aarch64-linux-gnu.tar.xz -C /usr/ARM/toolchain/
    echo "export PATH=\"/usr/ARM/toolchain/gcc-linaro-7.3.1-2018.05-x86_64_aarch64-linux-gnu/bin:\$PATH\"" >> ~/.bashrc
    source ~/.bashrc
  fi 
  apt-get -y install gcc-aarch64-linux-gnu g++-aarch64-linux-gnu libasound2-dev libasound2  libglib2.0-dev libgtk2.0-dev libpixman-1-dev pkg-config libexpat1-dev texinfo m4 flex bison python python2.7 python2.7-dev  libbabeltrace1 curl make gcc g++ libncurses5-dev libssl-dev libsdl2-dev

  #check whether there exists the tar packet,if not then install and decompress the qemu 4.1.1 
  if [ -e qemu-4.1.1.tar.xz ] 
  then
    echo "qemu-4.1.1.tar.xz exits, does not compile and install"
  else
    wget https://download.qemu.org/qemu-4.1.1.tar.xz 
    tar -xvf qemu-4.1.1.tar.xz
    cd  qemu-4.1.1
    ./configure --prefix=/usr/local/ --enable-sdl --enable-debug 
    make -j8
    make install
    cd -
  fi

  if [ -e QEMU_EFI.fd ]
  then 
    echo "QEMU_EFI.fd exists, do not download "
    qemu-img create -f qcow2 qemu_Euler.img 50G    
  else
    curl -sL https://releases.linaro.org/components/kernel/uefi-linaro/latest/release/qemu64/QEMU_EFI.fd -o QEMU_EFI.fd
    qemu-img create -f qcow2 qemu_Euler.img 50G    
  fi
}
  
prepare
echo "Done"
